//
//  MovieViewController.m
//  DouBan
//
//  Created by dllo on 16/6/23.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieTableViewCell.h"
#import "MovieModel.h"
#import "SecondMovieViewController.h"

@interface MovieViewController ()<UITableViewDelegate, UITableViewDataSource>
/** 第一个页面 */
@property (nonatomic, retain) UITableView *movieTableView;

/** 第二个页面 */
@property (nonatomic, retain) UICollectionView *movieCollectionView;

/** 数据源数组 */
@property (nonatomic, retain) NSMutableArray *movieDataSource;

/** 页面跳转动画对象 */
@property (nonatomic, retain) CATransition *transition;

@end

@implementation MovieViewController

static NSString *const movieTableViewCell = @"movieTableViewCell";

- (void)dealloc
{
    [_movieTableView release];
    [_movieCollectionView release];
    [_movieDataSource release];
    [_transition release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //加载动画对象
    [self createCATransitionObject];
    
    [self dataHandle];
    
    [self createMovieTableView];
    
    [self createRightBarButton];
}

//创建页面跳转动画对象
- (void)createCATransitionObject
{
    //初始化
    //CATransition 继承于CAAnimation    animation遍历构造器方法属于父类
    self.transition = [CATransition animation];
    
    //设置动画时长, 设置代理人
    self.transition.duration = 1.0f;
//    self.transition.delegate = self;
    /**
     枚举值
     kCAMediaTimingFunctionLinear
     kCAMediaTimingFunctionEaseIn
     kCAMediaTimingFunctionEaseOut
     kCAMediaTimingFunctionEaseInEaseOut
     kCAMediaTimingFunctionDefault
     */
    //设置切换速度效果
    self.transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    /**
     枚举值:
     kCATransitionFade = 1,    // 淡入淡出
     kCATransitionPush,        // 推进效果
     kCATransitionReveal,      // 揭开效果
     kCATransitionMoveIn,      // 慢慢进入并覆盖效果
     */
    //动画切换风格
    self.transition.type = kCATransitionFade;
    
    //动画切换方向
    /**
     kCATransitionFromRight//右侧
     kCATransitionFromLeft//左侧
     kCATransitionFromTop//顶部
     kCATransitionFromBottom//底部
     */
    self.transition.subtype = kCATransitionFromTop;//顶部
}

- (void)createRightBarButton
{
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickedRightBarButton:)];
    rightBarButtonItem.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;

}
- (void)didClickedRightBarButton:(UIBarButtonItem *)rightBarButton
{
    NSLog(@"第二个页面");
    //进行跳转 跳转动画一定设置为NO,不然会两个效果叠加
    [self.navigationController.view.layer addAnimation:_transition forKey:nil];
    SecondMovieViewController *smvc = [[SecondMovieViewController alloc] init];
    smvc.modelArray = [NSMutableArray arrayWithArray:_movieDataSource];
    
    [self.navigationController pushViewController:smvc animated:NO];
    
    [smvc release];

}

- (void)dataHandle
{
    self.movieDataSource = [NSMutableArray array];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSArray *dataArray = [dictionary objectForKey:@"result"];
    for (NSDictionary *dict in dataArray) {
        MovieModel *movieModel = [[MovieModel alloc] init];
        [movieModel setValuesForKeysWithDictionary:dict];
        [self.movieDataSource addObject:movieModel];
        [movieModel release];
    }
    
}

- (void)createMovieTableView
{
    self.movieTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44) style:UITableViewStylePlain];
    self.movieTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_movieTableView];
    [_movieTableView release];
    
    self.movieTableView.delegate = self;
    self.movieTableView.dataSource = self;
    
    [self.movieTableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:movieTableViewCell];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.movieDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieModel *model = [self.movieDataSource objectAtIndex:indexPath.row];
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:movieTableViewCell forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
